<?php
namespace Admin\Controller;

class ManageController extends AdminController{
	
	private $_model = null; //数据库操作类对象
	private $_role = null; //角色表操作类对象
	private $_user_model = null; //用户——角色表操作类对象

    //初始化操作
    public function __construct(){
    	parent::__construct();
    	$this->_model = D('adminuser');
	   	$this->_role = D('role');
		$this->_user_role = D('user_role');
    }
	public function index(){
		$list=$this->_model->order('id desc')->select();
		// dump($list);
		$arr=array();
		//遍历用户信息,其中需要用户的角色名称，依据角色表，用户表，角色用户表
		foreach($list as $v){
		  $role_ids=$this->_user_role->field('rid')->where(array('uid'=>array('eq',$v['id'])))->select();
		  //定义空的数组
		  $roles=array();
		  //遍历
		  foreach ($role_ids as $value) {
		  	$roles[]=$this->_role->where(array('id'=>array('eq',$value['rid']),'status'=>array('eq',1)))->getField('name');
		  }
		  $v['role']=$roles; ////将新得到角色信息放置到$v中
		  $arr[]=$v;
		}
	  	$this->assign("list",$arr);
		//加载模板
		$this->display();
	}
	public function del(){
		$id=I('get.id/d',0);
		if($this->_model->delete($id)>0 && $this->_user_role->where(array('uid'=>array('eq',$id)))>0){
			$this->success('删除成功!',U("Manage/index"));
		}else{
			$this->error("删除失败");
		}
	}
	//加载修改页面edit
	public function edit(){
		$id=I('get.id/d',0);
		$vo=$this->_model->find($id);
		if($vo===null){
			$this->redirect('Manage/index');
			rerutn;
		}
		$this->assign('vo',$vo);
        $this->display();
	} 
	public function update(){
		$this->_model->create();
		// if($this->_model->save()>0){
		// 	$this->success('修改成功',U('Manage/index'));
		// }else{
		// 	$this->error('修改失败');
		// }
		if($this->_model->save()===false){
			$this->error('修改失败');
		}else{
		   $this->success('修改成功',U('Manage/index'));
		}
	}
	public function add(){
		$this->display();
	}
	public function doadd(){
		// $this->_model->create();
		if(!$this->_model->create()){
			$this->error($this->_model->getError());
		}
		if($this->_model->add()>0){
			// echo $this->_model->getlastsql();exit;
			 $this->success('添加成功',U('Manage/index'));
			}else{
			 $this->error('添加失败');
			}
	}
	//角色浏览
	public function rolelist(){
		$id=I('get.id/d',0);
		//查询角色信息
		$list=$this->_role->where('status=1')->select();
		// dump($list);
		// dump($this->_role->getlastsql());
		//查询用户信息
		$users=$this->_model->where(array('id'=>array('eq',$id)))->find();
		// dump($users);
		// dump($this->_model->getlastsql());
		//查询当前用户的角色信息
		$rolelist=$this->_user_role->where(array('uid'=>array('eq',$id)))->select();
		// dump($rolelist);
		// dump($this->_user_role->getlastsql());
		$myrole=array();
		//拿出角色的id
		foreach($rolelist as $v){
			$myrole[]=$v['rid'];
		}
		$this->assign('list',$list);
		$this->assign('users',$users);
 		$this->assign('role',$myrole);
 		//加载模板
 		$this->display();
	}
	public function saverole(){
		// var_dump($_POST);exit;
		//将选中的角色添加角色权限表里(已知角色id,uid)
		if(empty($_POST['role'])){
			$this->error("请选择一个角色");
		}
		$uid=I('post.uid');
		// dump(I('role'));
		$this->_user_role->where(array('uid'=>array('eq',$uid)))->delete();
		foreach(I('role') as $v){
			$data['uid']=$uid;
			$data['rid']=$v;
			//执行添加
			$this->_user_role->data($data)->add();
		}
		$this->success('角色分配成功',U('Manage/index'));
	}

}